package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.YunGuoAttenceRecordsAdapter;
import com.iexbuy.ihg.adapter.YunJiSuanResultRecordsAdapter;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.YunItemListObject;
import com.iexbuy.ihg.entity.YunJiSuanResultObject;
import com.iexbuy.ihg.entity.YunJiSuanResultObjectResponse;
import com.iexbuy.ihg.entity.YunLuckAttandUserListObject;
import com.iexbuy.ihg.entity.YunLuckAttandUserListObjectResponce;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class YunGuoCalculationRecordsFragment extends BaseFragment {
	
	private static YunGuoCalculationRecordsFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;

	@ViewInject(R.id.lastBuyTime)
	private TextView lastBuyTime;
	@ViewInject(R.id.calculationResult)
	private TextView calculationResult;

	private static YunItemListObject yunItemListObject;
	private static int period;

	private List<YunJiSuanResultObject> yunJiSuanResultObjects = new ArrayList<YunJiSuanResultObject>();
	private int pageNo = 0;
	private YunJiSuanResultRecordsAdapter yunJiSuanResultRecordsAdapter;

	private View listHeaderView;
	private View listFooterView;

	private String lastAttenTime;
	private int sum = 0;
	private int totalAtten = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_yunguo_calculation_records, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.yun_guo_calculat_records));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		listHeaderView = mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_yun_jisuan_header, null);
		ViewUtils.inject(this, listHeaderView);
		listFooterView = mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_yun_jisuan_footer, null);
		ViewUtils.inject(this, listFooterView);

		listView.addHeaderView(listHeaderView);
		listView.addFooterView(listFooterView);

		initAttenceRecordsData();
	}


	public void initAttenceRecordsData() {
		if (yunItemListObject == null) return;

		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("itemId", yunItemListObject.getItemId());
		paramMap.put("period", String.valueOf(period));
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_JI_SUAN_RESULT, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						YunJiSuanResultObjectResponse rep = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), YunJiSuanResultObjectResponse.class);
						if (pageNo == 0) {
							yunJiSuanResultObjects.clear();
						}
						yunJiSuanResultObjects.addAll(rep.getAttendanceList());
						totalAtten = Integer.valueOf(rep.getAttendanceNumber());
						initAttenceRecordsView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
					}
				});
	}

	private void initAttenceRecordsView() {
		initListHeaderFooter();

		if (yunJiSuanResultRecordsAdapter == null) {
			yunJiSuanResultRecordsAdapter = new YunJiSuanResultRecordsAdapter(mBaseActivity, yunJiSuanResultObjects);
			listView.setAdapter(yunJiSuanResultRecordsAdapter);
		}
		yunJiSuanResultRecordsAdapter.notifyDataSetChanged();
	}

	private void initListHeaderFooter() {
		Collections.sort(yunJiSuanResultObjects, new Comparator<YunJiSuanResultObject>() {
			@Override
			public int compare(YunJiSuanResultObject yunJiSuanResultObject, YunJiSuanResultObject t1) {
				return yunJiSuanResultObject.getTime().compareTo(t1.getTime());
			}
		});
		lastAttenTime =  yunJiSuanResultObjects.get(0).getTime();
		for (YunJiSuanResultObject yunJiSuanResultObject : yunJiSuanResultObjects) {
			sum += Integer.valueOf(yunJiSuanResultObject.getData());
		}

		int mod = sum % totalAtten;
		int result = mod + 10000000;

		lastBuyTime.setText("最后购买时间 "+lastAttenTime);
		calculationResult.setText("取以上数值计算步骤\\n1、求和："+sum+"(以上100条0购记录时间取值之各)\\n2、取余数："+sum+"%"+totalAtten+"（本商品总参与人次）＝ "+mod+"（余数）\\n3、计算结果："+mod+" + 10000000 = " + result);
	}


	public static void setYunItemListObject(YunItemListObject yunItemListObject) {
		YunGuoCalculationRecordsFragment.yunItemListObject = yunItemListObject;
	}

	public static void setPeriod(int period) {
		YunGuoCalculationRecordsFragment.period = period;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static YunGuoCalculationRecordsFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
